######## Custom Search Filters ########
######## Final Year Project 2011#####
######## Making Web Search Spatial###


######## Liam Sullivan       ########
######## University of Leeds ########

import wsgiref.handlers
import urllib
import logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch

from django.utils import simplejson


from StringIO import StringIO

from operator import itemgetter



import re
from google.appengine.ext import webapp
 
register = webapp.template.create_template_register()
 
def escapeimg(body):
    return re.sub(r'&lt;img (.*)/&gt;', '[IMG]', body)

def echothis(body):
    """Test for filters - just echos input with echo suffix"""
    body = body + " ECHO!"
    output = str(body)
    
    return output



def getDivID(rel, NumberOfResults):
    """Takes a relevance number and returns the mapped Div label, depends on how many results have been requested in total"""
    if rel <2:
        #Top 10 results!
        output =  "one"
    
    elif rel <3:
        #Top 20 results!
        output = "two"
        
    elif rel <4:
        #Top 20 results!
        
        output = "three"
        
    elif rel <5:
        #Top 20 results!
        output = "four"
        
    elif rel <6:
        #Top 20 results!
        output = "five"
        
    elif rel <7:
        #Top 20 results!
        output = "six"
    
    elif rel <8:
        #Top 20 results!
        output = "seven"
    
    elif rel <10:
        #Top 20 results!
        output = "eight"
    
    elif rel <10:
        #Top 20 results!
        output = "nine"
        
    elif rel <11:
        #Top 20 results!
        output = "ten"

    
    return output



def getClassificationData(URL):
    """Classifies a URL using the UClassify Service"""
    
    #Dictionary containing all params, which we will encode for REST Call    
    Params = {}   
        
    #BaseURL
    #uClassify URL Endpoint
    endpoint = "http://uclassify.com/browse/uClassify/Topics/ClassifyUrl?"
    
    #API Key    
    Params['readkey'] = "TsE3A35FCBSAdbGFLEvI2PXH0"
    
    #Output Format
    Params['output'] = "json"
    
    #Incoming URL that needs classifying
    Params['url'] = URL
    
    #Version
    Params['version'] = 1.01
    
    #Encode Params from dictionary for call
    
    RestParams = urllib.urlencode(Params)    
    
    #Combine base url and params
    
    RestURL = endpoint + RestParams
    
    print "<h1>" + RestURL + "</h1>"
    
    response = StringIO(urlfetch.fetch(RestURL).content)    
    
    result = simplejson.load(response)
    
    print "Content-Type: text/html"
    #for item in result.iterkeys():
        #print "<h1>" + item + "/h1>"
    
    ResultSet =  result['cls1'] 
    
    
    
    #ResultSet = result        
    
    return ResultSet
    


def ClassifyURL(URL):
    
    """Classifies a URL, calls other method to get the results from the Web Service then computes highest probability and returns as a string"""

    #Pass URL and get the dictionary results from the classifier
    Results = getClassificationData(URL)
    
    #Take the first key from a sorted set of the dictionary, this is the most likely topic as classified
    
    SortedResults = sorted(Results.items(), key=itemgetter(1), reverse=True)
    
    TopResult = SortedResults[0]
    
    return TopResult[0]


def StringMe(input):
    
    """Returns a string"""
    
    logging.info("Input was: ")
    logging.info(input)
    logging.info("And string is: ")
    logging.info(str(input))
    return str(input)

def TruncateTitle(s):
    """Truncates sentence - to keep titles short, change integer here to alter length of titles"""
    
    return ' '.join(s.split()[:4])


#Register filters for use in Django template language 
register.filter(StringMe) 
register.filter(ClassifyURL) 
register.filter(echothis)
register.filter(escapeimg)
register.filter(getDivID)
register.filter(TruncateTitle)

